package de.hsf.bp.netdoc.gui;

import javax.imageio.ImageIO;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JPopupMenu;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.Component;

import javax.swing.JSeparator;

public class Docu extends JPanel {
	
	GuiView myGUI; 
	

	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		Graphics2D g2d = (Graphics2D) g;
        for (Shape s : shapes) {
            g2d.draw(s);
        }		
	     try {
	          Image img = ImageIO.read(new File("netz.png"));
	          g.drawImage(img, 0, 0, this);
	         g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
	        } catch (IOException e) {
	          e.printStackTrace();
	          }
	    g.drawOval(832, 355, 13, 13);
	   g.drawLine(175, 185, 258, 210);
	    g.drawString("CR1", 98, 165);
	   
	     
	        }
	private static ArrayList<Shape> shapes;
	 
	private Shape CR1 = new Ellipse2D.Double(85, 154, 13, 13);
	private Shape CR2 = new Ellipse2D.Double(85, 197, 13, 13);
	private Shape CR3 = new Ellipse2D.Double(85, 215, 13, 13);
	private Shape CR4 = new Ellipse2D.Double(85, 255, 13, 13);
	private Shape CR5 = new Ellipse2D.Double(85, 275, 13, 13);
	private Shape CR6 = new Ellipse2D.Double(86, 314, 13, 13);
	private Shape CR7 = new Ellipse2D.Double(86, 332, 13, 13);
	private Shape CR8 = new Ellipse2D.Double(85, 360, 13, 13);
	
	private Shape CAS1 = new Ellipse2D.Double(170, 180, 13, 13);
	private Shape CAS2 = new Ellipse2D.Double(170, 230, 13, 13);
	private Shape CAS3 = new Ellipse2D.Double(170, 295, 13, 13);
	private Shape CAS4 = new Ellipse2D.Double(170, 350, 13, 13);
	
	private Shape DS1 = new Ellipse2D.Double(253, 206, 13, 13);
	private Shape DS2 = new Ellipse2D.Double(253, 318, 13, 13);
	
	private Shape CS1 = new Ellipse2D.Double(375, 233, 13, 13);
	private Shape CS2 = new Ellipse2D.Double(375, 298, 13, 13);
	
	private Shape GS1 = new Ellipse2D.Double(496, 181, 13, 13);
	private Shape GS2 = new Ellipse2D.Double(496, 233, 13, 13);
	private Shape GS3 = new Ellipse2D.Double(496, 296, 13, 13);
	private Shape GS4 = new Ellipse2D.Double(496, 335, 13, 13);
	
	private Shape SAS1 = new Ellipse2D.Double(580, 181, 13, 13);
	private Shape SAS2 = new Ellipse2D.Double(580, 232, 13, 13);
	private Shape SAS3 = new Ellipse2D.Double(580, 297, 13, 13);
	private Shape SAS4 = new Ellipse2D.Double(580, 355, 13, 13);
	
	private Shape pS1 = new Ellipse2D.Double(664, 159, 13, 13);
	private Shape pS2 = new Ellipse2D.Double(664, 180, 13, 13);
	private Shape pS3 = new Ellipse2D.Double(664, 200, 13, 13);
	private Shape pS4 = new Ellipse2D.Double(663, 218, 13, 13);
	private Shape pS5 = new Ellipse2D.Double(663, 261, 13, 13);
	private Shape pS6 = new Ellipse2D.Double(664, 285, 13, 13);
	private Shape pS7 = new Ellipse2D.Double(664, 317, 13, 13);
	private Shape pS8 = new Ellipse2D.Double(664, 334, 13, 13);
	private Shape pS9 = new Ellipse2D.Double(664, 365, 13, 13);
	
	private Shape vSw1 = new Ellipse2D.Double(748, 180, 13, 13);
	private Shape vSw2 = new Ellipse2D.Double(748, 217, 13, 13);
	private Shape vSw3 = new Ellipse2D.Double(748, 285, 13, 13);
	private Shape vSw4 = new Ellipse2D.Double(748, 335, 13, 13);
	
	private Shape vS1 = new Ellipse2D.Double(833, 157, 13, 13);
	private Shape vS2 = new Ellipse2D.Double(832, 193, 13, 13);
	private Shape vS3 = new Ellipse2D.Double(832, 210, 13, 13);
	private Shape vS4 = new Ellipse2D.Double(832, 241, 13, 13);
	private Shape vS5 = new Ellipse2D.Double(832, 267, 13, 13);
	private Shape vS6 = new Ellipse2D.Double(832, 302, 13, 13);
	private Shape vS7 = new Ellipse2D.Double(832, 321, 13, 13);
	private Shape vS8 = new Ellipse2D.Double(832, 355, 13, 13);
	
	
	
	
	
	
   
    
   
	
	/**
	 * Create the panel.
	 */
	/**
	 * @param gui
	 */
    public Docu(GuiView gui) {
    	
       	myGUI = gui;
		
		JButton btnBack = new JButton("back");
		btnBack.setBounds(791, 516, 89, 23);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				myGUI.backToStart();
			}
		});
		setLayout(null);
		
		
		final JPopupMenu menu = new JPopupMenu();
		addPopup(this, menu);
		
		JMenuItem mntmAdd = new JMenuItem("Add");
		menu.add(mntmAdd);
		
		JMenuItem mntmDelete = new JMenuItem("Delete");
		menu.add(mntmDelete);
		
		JSeparator separator = new JSeparator();
		menu.add(separator);
		
		JMenuItem mntmBearbeiten = new JMenuItem("Bearbeiten");
		mntmBearbeiten.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Eigenschaften JDialog;
				JDialog = new Eigenschaften();
				JDialog.show();	
				JDialog.setVisible(true);
				JDialog.setLocationRelativeTo(myGUI);
				JDialog.setModalityType(null);
			}
		});
		menu.add(mntmBearbeiten);
		
		JMenuItem mntmEigentschaften = new JMenuItem("Eigentschaften");
		menu.add(mntmEigentschaften);
		
		JSeparator separator_1 = new JSeparator();
		menu.add(separator_1);
		
		JMenuItem mntmClearAll = new JMenuItem("Clear all");
		menu.add(mntmClearAll);
		add(btnBack); 
	      
	      
        shapes = new ArrayList<>();
        shapes.add(CR1);
        shapes.add(CR2);
        shapes.add(CR3);
        shapes.add(CR4);
        shapes.add(CR5);
        shapes.add(CR6);
        shapes.add(CR7);
        shapes.add(CR8);
        shapes.add(CAS1);
        shapes.add(CAS2);
        shapes.add(CAS3);
        shapes.add(CAS4);
        shapes.add(DS1);
        shapes.add(DS2);
        shapes.add(CS1);
        shapes.add(CS2);
        shapes.add(GS1);
        shapes.add(GS2);
        shapes.add(GS3);
        shapes.add(GS4);
        shapes.add(SAS1);
        shapes.add(SAS2);
        shapes.add(SAS3);
        shapes.add(SAS4);
        shapes.add(CS1);
        shapes.add(CS2);
        shapes.add(pS1);
        shapes.add(pS2);
        shapes.add(pS3);
        shapes.add(pS4);
        shapes.add(pS5);
        shapes.add(pS6);
        shapes.add(pS7);
        shapes.add(pS8);
        shapes.add(pS9);
        shapes.add(vSw1);
        shapes.add(vSw2);
        shapes.add(vSw3);
        shapes.add(vSw4);
        shapes.add(vS1);
        shapes.add(vS2);
        shapes.add(vS3);
        shapes.add(vS4);
        shapes.add(vS5);
        shapes.add(vS6);
        shapes.add(vS7);
        shapes.add(vS8);
        
        
      
    }
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				
				for (Shape s : shapes) {
					
				if (s.contains(e.getPoint())) {//check if mouse is clicked within shape
					
					showMenu(e);
									} 
					}
			}
			
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	 	}
